#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Agreements 
{
	/// <summary>
	/// Summary description for Annotation.
	/// </summary>
	public class Annotations : BO.SaveableBusinessObject
	{
		#region Constants
		private static string AnnotationsKey = GenerateKey.MakeKey("Annotations_C5F30070-B7B9-4d86-896A-E4D2A585DDA5",GenerateKey.Agreement);
		#endregion

		#region Constructor
		/// <summary>
		///  Constructor
		/// </summary>
		public Annotations(DataSet dataSet, ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = dataSet;
			CreateCommands();
		}

		#region private members
		private const string AnnotationsDataTableName = "Annotations";
		private SqlCommand	addCommand;
		#endregion

		/// <summary>
		/// overloaded constructor that takes a dataset
		/// and sets its dataset as the one passed in.
		/// </summary>
		public Annotations (ICachePolicy cachePolicy, DataSet dataSet)
			: base(cachePolicy)
		{
			DataContainer = dataSet;
		}
		#endregion
		
		#region Base Methods
		public override void Cache()
		{
			CachePolicy.Cache(AnnotationsKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(AnnotationsKey);
		}

		public override void Load( string connectionString, params object[] args )
		{
			if ( args.Length != 1 )
				throw new Exception("Annotation Load expects Annotation ID.");

			int annotationContainerId = (int)args[0];

			try
			{
				Utility.DataAccess.LoadDataSet(DataContainer, AnnotationsDataTableName, connectionString, "GetAnnotations", 
					Utility.DataAccess.BuildSqlParameterArray("@annAnnotationContainerId" ,SqlDbType.Int, annotationContainerId));
			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("Annotation Load", e, true, Utility.ExceptionReport.ReportType.Error);
			}
		}

		/// Saveable Object 
		/// 
		public override void Save(string connectionString, params object[] args)
		{
			// currently can only insert new ones.
			ExecuteInsert( connectionString );
		}

		/// <summary>
		/// Execute insert will search the Datagrid for any new entries, 
		/// and save them
		/// </summary>
		/// <param name="connectionString">connection String to the DB</param>
		private void ExecuteInsert ( string connectionString )
		{
			// create a connection
			SqlConnection connection = new SqlConnection(connectionString);
			addCommand.Connection = connection;				
			SqlDataAdapter adapter = new SqlDataAdapter();
			adapter.InsertCommand = addCommand;
			adapter.Update(DataContainer, "Annotations");
		}

		private void CreateCommands( )
		{
			addCommand = new SqlCommand();
			addCommand.CommandText = "AddUserAnnotation";
			addCommand.CommandType = CommandType.StoredProcedure;
			addCommand.CommandTimeout = Convert.ToInt32(Utility.Settings.GetKeyValue("Database.Insight.CommandTimeout", "0"));
			addCommand.Parameters.Add("@annotationContainerId", SqlDbType.Int).SourceColumn = "AnnotationContainerId";
			addCommand.Parameters.Add("@annotationTypeId", SqlDbType.Int).SourceColumn = "AnnotationTypeId";
			addCommand.Parameters.Add("@title", SqlDbType.VarChar).SourceColumn = "Title";
			addCommand.Parameters.Add("@detail", SqlDbType.VarChar).SourceColumn = "Detail";
			addCommand.Parameters.Add("@sbsOriginatorTypeId", SqlDbType.Int).SourceColumn = "SbsOriginatorTypeId";
			addCommand.Parameters.Add("@fwkApplicationUserId", SqlDbType.Int).SourceColumn = "FwkApplicationUserId";
		}
		#endregion
	}
}
